//
//  ArticleTableViewController.swift
//  TableCellAnimation
//
//  Created by Simon Ng on 3/10/2016.
//  Copyright © 2016 AppCoda. All rights reserved.
//

import UIKit

class ArticleTableViewController: UITableViewController {
    
    let postTitles = ["Use Background Transfer Service To Download File in Background",
                      "Face Detection in iOS Using Core Image",
                      "Building a Speech-to-Text App Using Speech Framework in iOS 10",
                      "Building Your First Web App in Swift Using Vapor",
                      "Creating Gradient Colors Using CAGradientLayer",
                      "A Beginner's Guide to CALayer"];
    let postImages = ["imessage-sticker-pack", "face-detection-featured", "speech-kit-featured", "vapor-web-framework", "cagradientlayer-demo", "calayer-featured"];

    var showneds:[Bool] = []
    
    func flyIn(cell:UITableViewCell){
        let transform = CATransform3DTranslate(CATransform3DIdentity, -500, 100, 0)
        cell.layer.transform = transform
        
        UIView.animate(withDuration: 1.0){
            cell.layer.transform = CATransform3DIdentity
        }
    }
    
    func rotate(cell:UITableViewCell){
        let rotationAngleInRadians = 90.0 * CGFloat(Double.pi/180.0)
        let rotationTransform = CATransform3DMakeRotation(rotationAngleInRadians, 0, 0, 1)
        cell.layer.transform = rotationTransform
        
        UIView.animate(withDuration: 1.0){
            cell.layer.transform = CATransform3DIdentity
        }
    }
    
    func fadeIn(cell:UITableViewCell){
        cell.alpha = 0
        UIView.animate(withDuration: 1.0){
            cell.alpha = 1
        }
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //fadeIn(cell: cell)
        //rotate(cell: cell)
        //flyIn(cell: cell)
        /*if cell.tag == 0{
            cell.tag = 1
            flyIn(cell: cell)
        }*/
        /*
        if let myCell = cell as? ArticleTableViewCell{
            if myCell.firstShown == false{
                myCell.firstShown = true
                flyIn(cell: cell)
            }
        }*/
        
        if showneds[indexPath.row] == true{
            return
        }
        
        showneds[indexPath.row] = true
        flyIn(cell: cell)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.estimatedRowHeight = 258.0
        tableView.rowHeight = UITableViewAutomaticDimension
        
        showneds = Array<Bool>(repeating:false,count:postTitles.count)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return postTitles.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ArticleTableViewCell
        
        // Configure the cell...
        cell.titleLabel.text = postTitles[(indexPath as NSIndexPath).row]
        cell.postImageView.image = UIImage(named: postImages[(indexPath as NSIndexPath).row])

        return cell
    }
    

}
